package com.anyikang.volunteer.sos.volunteer.app.sos;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.anyikang.volunteer.sos.volunteer.R;
import com.anyikang.volunteer.sos.volunteer.app.spot.SpotFragment;
;

import java.util.ArrayList;

/**
 * @author xw.gao
 * @version 2017/6/17
 * @git http://60.205.139.138:81/root/GSMServerAPP.git
 *
 */
public class SosListFragment extends Fragment
{
	Resources resources;
	private ViewPager mPager;
	private ArrayList<Fragment> fragmentsList;
	private ImageView ivBottomLine;
	private TextView tvTabAllHelp,tvTabNeedHelp,tvTabHelping, tvTabHelpComplete;
	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	public final static int num = 2 ;

    String TAG = "SosListFragment";
	private View layout;
	private Activity mActivity;
	private ImageView underLine;
	private int one;
	MyFragmentPagerAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO: inflate a fragment view

		layout = inflater.inflate(R.layout.fragment_sos_list, container, false);
		initView();
		Log.i(TAG,"onCreateView");
		return layout;
	}

	@Override
	public void onDestroyView() {
		Log.i(TAG,"onDestroyView");
		super.onDestroyView();
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		Log.i(TAG,"onHiddenChanged="+hidden);
		super.onHiddenChanged(hidden);
	}

	@Override
	public void onAttach(Context context) {
		Log.i(TAG,"onAttach");
		mActivity = (Activity) context;
		super.onAttach(context);
	}

	@Override
	public void onStart() {
		Log.i(TAG,"onStart");
		super.onStart();
	}

	@Override
	public void onResume() {
		Log.i(TAG,"onResume");
		super.onResume();
	}

	@Override
	public void onStop() {
		Log.i(TAG,"onStop");
		super.onStop();
	}

	/**
	 * 初始化顶部标签栏 的 下划线
	 */
	private void initUnderLineImage() {

		underLine = (ImageView) layout.findViewById(R.id.cursor);
/*		int bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.cursor)
                .getWidth();*/
		DisplayMetrics dm = new DisplayMetrics();
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int bmpW = screenW / 4; //2改为4

		int offset = (screenW / 4-bmpW) / 2;
		one = offset * 2 + bmpW;// 两个相邻页面的偏移量

		ViewGroup.LayoutParams para;
		para = underLine.getLayoutParams();
		para.height = 2;
		para.width = screenW / 4;
		underLine.setLayoutParams(para);

		// imgageview设置平移，使下划线平移到初始位置（平移一个offset）
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		underLine.setImageMatrix(matrix);
		int colorYellow = this.getResources().getColor(R.color.colorPrimary);
		underLine.setBackgroundColor(colorYellow);
	}



	/**
	 * 选中的标签字体颜色变红色，同时进度线平移动到该标签下
	 *
	 * @param position
	 *  第几个标签
	 *
	 */
	private void setTitleTag(int position) {

		Animation animation = new TranslateAnimation(currIndex * one, position
				* one, 0, 0);// 平移动画
		currIndex = position;
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(200);// 动画持续时间0.2秒
		underLine.startAnimation(animation);// 是用ImageView来显示动画的

		Resources rs = this.getResources();
		int colorRed = rs.getColor(R.color.colorPrimary);
		int colorGray = rs.getColor(R.color.text_describe);

		switch (position) {
			case 0:  //所有
				tvTabAllHelp.setTextColor(colorRed);
				tvTabNeedHelp.setTextColor(colorGray);
				tvTabHelping.setTextColor(colorGray);
				tvTabHelpComplete.setTextColor(colorGray);
				break;
			case 1://需要救助
				tvTabAllHelp.setTextColor(colorGray);
				tvTabNeedHelp.setTextColor(colorRed);
				tvTabHelping.setTextColor(colorGray);
				tvTabHelpComplete.setTextColor(colorGray);
				break;

			case 2:  //救助中
				tvTabAllHelp.setTextColor(colorGray);
				tvTabNeedHelp.setTextColor(colorGray);
				tvTabHelping.setTextColor(colorRed);
				tvTabHelpComplete.setTextColor(colorGray);
				break;
			case 3: //救助已完成
				tvTabAllHelp.setTextColor(colorGray);
				tvTabNeedHelp.setTextColor(colorGray);
				tvTabHelping.setTextColor(colorGray);
				tvTabHelpComplete.setTextColor(colorRed);
				break;
		}
	}

	/**
	 * 实例化 视图控件
	 */
	private void initView() {

		TextView tvTitle = (TextView) layout.findViewById(R.id.tv_title);
		tvTitle.setText("呼救列表");
		ImageButton imgBtLeft = (ImageButton) layout.findViewById(R.id.imgBtLeft);
		imgBtLeft.setVisibility(View.GONE);

		tvTabAllHelp = (TextView) layout.findViewById(R.id.tvAllHelp);
		tvTabNeedHelp = (TextView) layout.findViewById(R.id.tvNeedHelp);
		tvTabHelping = (TextView) layout.findViewById(R.id.tvHelping);
		tvTabHelpComplete = (TextView) layout.findViewById(R.id.tvHelpComplete);


		tvTabAllHelp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mPager.setCurrentItem(0);
			}
		});


		tvTabNeedHelp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mPager.setCurrentItem(1);
			}
		});


		tvTabHelping.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mPager.setCurrentItem(2);
			}
		});


		tvTabHelpComplete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mPager.setCurrentItem(3);
			}
		});

		initUnderLineImage();
		setTitleTag(0); //默认钻中进行中的问卷

		//VIEWPAGER,具体数据在fragment
		mPager = (ViewPager) layout.findViewById(R.id.vPager);


		mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager()));
		mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}
			@Override
			public void onPageSelected(int position) {

				setTitleTag(position);

			}
			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
		mPager.setCurrentItem(0);
	}
}
